<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>批量发货</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        :root {
            --primary-color: #1989fa;
            --bg-color: #f5f5f5;
            --card-bg: #fff;
            --text-color: #333;
            --border-color: #eee;
            --success-color: #52c41a;
        }
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: var(--bg-color);
            color: var(--text-color);
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }
        .header {
            padding: 15px;
            background-color: var(--primary-color);
            color: white;
            display: flex;
            align-items: center;
            position: sticky;
            top: 0;
            z-index: 100;
        }
        .back-btn {
            background: none;
            border: none;
            color: white;
            padding: 0 15px;
            font-size: 16px;
            cursor: pointer;
        }
        .header-title {
            flex: 1;
            text-align: center;
            font-size: 18px;
            margin-right: 45px;
        }
        .main-content {
            flex: 1;
            padding: 15px;
        }
        .batch-form {
            background: white;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 15px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        .section-title {
            font-size: 15px;
            font-weight: 500;
            margin-bottom: 12px;
            color: #333;
            display: flex;
            align-items: center;
        }
        .section-title i {
            margin-right: 6px;
            color: var(--primary-color);
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-label {
            display: block;
            margin-bottom: 8px;
            font-size: 14px;
            color: #333;
        }
        .form-select {
            width: 100%;
            padding: 8px 12px;
            border: 1px solid var(--border-color);
            border-radius: 4px;
            font-size: 14px;
            background-color: white;
        }
        .form-textarea {
            width: 100%;
            padding: 8px 12px;
            border: 1px solid var(--border-color);
            border-radius: 4px;
            font-size: 14px;
            min-height: 100px;
            resize: vertical;
            box-sizing: border-box;
        }
        .form-tip {
            font-size: 12px;
            color: #666;
            margin-top: 4px;
            line-height: 1.5;
        }
        .preview-list {
            background: white;
            border-radius: 8px;
            margin-bottom: 15px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            overflow: hidden;
        }
        .preview-header {
            padding: 12px 15px;
            background-color: #f8f8f8;
            font-size: 14px;
            color: #666;
            display: grid;
            grid-template-columns: 1fr 1fr 1fr;
            gap: 10px;
        }
        .preview-item {
            padding: 12px 15px;
            border-bottom: 1px solid var(--border-color);
            font-size: 14px;
            display: grid;
            grid-template-columns: 1fr 1fr 1fr;
            gap: 10px;
            align-items: center;
        }
        .preview-item:last-child {
            border-bottom: none;
        }
        .preview-error {
            color: #ff4d4f;
        }
        .submit-bar {
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            padding: 10px 15px;
            background: white;
            box-shadow: 0 -1px 3px rgba(0,0,0,0.1);
        }
        .submit-btn {
            width: 100%;
            padding: 12px;
            background-color: var(--primary-color);
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
        }
        .submit-btn:disabled {
            background-color: #ccc;
            cursor: not-allowed;
        }
        .empty-tip {
            text-align: center;
            color: #999;
            padding: 30px 0;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="header">
        <button class="back-btn" onclick="history.back()">
            <i class="fas fa-arrow-left"></i>
        </button>
        <div class="header-title">批量发货</div>
    </div>

    <div class="main-content">
        <div class="batch-form">
            <div class="section-title">
                <i class="fas fa-truck"></i>
                发货信息
            </div>
            <div class="form-group">
                <label class="form-label">快递公司</label>
                <select class="form-select" id="courierSelect">
                    <option value="">请选择快递公司</option>
                    <option value="sf">顺丰速运</option>
                    <option value="zt">中通快递</option>
                    <option value="yd">韵达快递</option>
                    <option value="ems">EMS</option>
                </select>
            </div>
            <div class="form-group">
                <label class="form-label">批量录入</label>
                <textarea class="form-textarea" id="batchInput" 
                    placeholder="请输入订单号和快递单号，每行一个，用空格或逗号分隔
例如：
2025063001 SF1234567890
2025063002,YT987654321"></textarea>
                <div class="form-tip">
                    格式说明：<br>
                    1. 每行输入一个订单的信息<br>
                    2. 订单号和快递单号用空格或逗号分隔<br>
                    3. 快递单号应为6-32位字母数字组合
                </div>
            </div>
        </div>

        <div class="preview-list">
            <div class="section-title" style="padding: 15px 15px 0;">
                <i class="fas fa-list-check"></i>
                发货预览
            </div>
            <div class="preview-header">
                <div>订单号</div>
                <div>快递单号</div>
                <div>状态</div>
            </div>
            <div id="previewContainer">
                <div class="empty-tip">请在上方输入框中录入订单信息</div>
            </div>
        </div>
    </div>

    <div class="submit-bar">
        <button class="submit-btn" id="submitBtn" disabled onclick="handleSubmit()">确认发货</button>
    </div>

    <script>
    let validItems = [];

    document.getElementById('batchInput').addEventListener('input', function(e) {
        const text = e.target.value;
        const lines = text.split('\n').filter(line => line.trim());
        const container = document.getElementById('previewContainer');
        const submitBtn = document.getElementById('submitBtn');
        
        // 清空预览区
        container.innerHTML = '';
        validItems = [];

        if (lines.length === 0) {
            container.innerHTML = '<div class="empty-tip">请在上方输入框中录入订单信息</div>';
            submitBtn.disabled = true;
            return;
        }

        lines.forEach(line => {
            const [orderNum, trackingNum] = line.split(/[,\s]+/);
            const isValid = validateInput(orderNum, trackingNum);
            
            const item = document.createElement('div');
            item.className = 'preview-item';
            item.innerHTML = `
                <div>${orderNum || '---'}</div>
                <div>${trackingNum || '---'}</div>
                <div class="${isValid ? '' : 'preview-error'}">
                    ${isValid ? '有效' : '格式错误'}
                </div>
            `;
            container.appendChild(item);

            if (isValid) {
                validItems.push({ orderNum, trackingNum });
            }
        });

        // 更新提交按钮状态
        submitBtn.disabled = validItems.length === 0;
    });

    function validateInput(orderNum, trackingNum) {
        const orderPattern = /^\d{10}$/;  // 10位数字
        const trackingPattern = /^[A-Za-z0-9]{6,32}$/;  // 6-32位字母数字
        return orderPattern.test(orderNum) && trackingPattern.test(trackingNum);
    }

    function handleSubmit() {
        const courier = document.getElementById('courierSelect').value;
        if (!courier) {
            alert('请选择快递公司');
            return;
        }

        if (validItems.length === 0) {
            alert('没有有效的发货信息');
            return;
        }

        // 这里添加批量发货的逻辑
        console.log('发货信息：', {
            courier,
            items: validItems
        });

        // 模拟发货成功
        alert(`成功发货 ${validItems.length} 个订单！`);
        history.back();
    }
    </script>
</body>
</html>